package org.linlinjava.litemall.admin.dto;

import com.alibaba.excel.context.AnalysisContext;
import com.alibaba.excel.read.listener.ReadListener;
import com.alibaba.excel.util.ListUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.linlinjava.litemall.admin.web.AdminAdController;
import org.linlinjava.litemall.db.domain.IliquormallLibrary;
import org.linlinjava.litemall.db.service.ILiquormallLibraryService;
import org.springframework.util.StringUtils;

import java.math.BigDecimal;
import java.util.List;
import java.util.logging.Logger;

public class LiquorLibraryListener implements ReadListener<LiquorLibraryExcelData> {

    private final Log logger = LogFactory.getLog(LiquorLibraryListener.class);

    private static final int BATCH_COUNT = 100;

    private List<IliquormallLibrary> cachedDataList = ListUtils.newArrayListWithExpectedSize(BATCH_COUNT);

    private ILiquormallLibraryService liquormallLibraryService;

    private int total;

    private boolean res;

    @Override
    public void invoke(LiquorLibraryExcelData liquorLibraryExcelData, AnalysisContext analysisContext) {
        IliquormallLibrary library = new IliquormallLibrary();
        if (StringUtils.isEmpty(liquorLibraryExcelData.getPicUrl())) {
            logger.error("此条酒库信息缺少numId：" + liquorLibraryExcelData.toString());
            return;
        }
        try {
            library.setNumId(Integer.valueOf(liquorLibraryExcelData.getPicUrl()));
        } catch (Exception e) {
            logger.error("此条酒库信息numId转换失败：" + liquorLibraryExcelData.toString());
            return;
        }
        library.setName(liquorLibraryExcelData.getName());
        library.setContry(liquorLibraryExcelData.getContry());
        library.setProvince(liquorLibraryExcelData.getProvince());
        library.setType(liquorLibraryExcelData.getType());
        library.setBrand(liquorLibraryExcelData.getBrand());
        library.setWinery(liquorLibraryExcelData.getWinery());
        library.setDescription(liquorLibraryExcelData.getDescription());
        library.setDelete(Byte.valueOf("0"));
        String picUrl = "https://oss.xuankab.top/library/"
                + analysisContext.readSheetHolder().getSheetName() + "/"
                + liquorLibraryExcelData.getPicUrl()
                + ".jpg";
        library.setPicUrl(picUrl);
        try {
            if (!StringUtils.isEmpty(liquorLibraryExcelData.getPrice())) {
                library.setPrice(new BigDecimal(liquorLibraryExcelData.getPrice()));
            }
        } catch (Exception e) {
            logger.error("此条酒库信息price信息有误：" + liquorLibraryExcelData.toString());
        }
        try {
            if (!StringUtils.isEmpty(liquorLibraryExcelData.getAlcohol())) {
                library.setAlcohol(Float.valueOf(liquorLibraryExcelData.getAlcohol()));
            }
        } catch (Exception e) {
            logger.error("此条酒库信息alcohol信息有误：" + liquorLibraryExcelData.toString());
        }
        try {
            if (!StringUtils.isEmpty(liquorLibraryExcelData.getCapacity())) {
                library.setCapacity(Integer.valueOf(liquorLibraryExcelData.getCapacity()));
            }
        } catch (Exception e) {
            logger.error("此条酒库信息capacity信息有误：" + liquorLibraryExcelData.toString());
        }

        cachedDataList.add(library);

        if (cachedDataList.size() >= BATCH_COUNT) {
            saveData();
            cachedDataList = ListUtils.newArrayListWithExpectedSize(BATCH_COUNT);
        }
    }

    @Override
    public void doAfterAllAnalysed(AnalysisContext analysisContext) {
        if (analysisContext.readSheetHolder().getSheetNo() == total - 1 && cachedDataList.size() != 0) {
            saveData();
        }
    }

    private void saveData() {
        liquormallLibraryService.batchInsertLiquorLibrary(cachedDataList);
    }

    public ILiquormallLibraryService getLiquormallLibraryService() {
        return liquormallLibraryService;
    }

    public void setLiquormallLibraryService(ILiquormallLibraryService liquormallLibraryService) {
        this.liquormallLibraryService = liquormallLibraryService;
    }

    public int getTotal() {
        return total;
    }

    public void setTotal(int total) {
        this.total = total;
    }
}
